package com.guigu.qingcheng.controller;

import com.alibaba.fastjson.JSONObject;
import com.guigu.qingcheng.pojo.TbOrderItem;
import com.guigu.qingcheng.pojo.dto.CaiFenDinDan;
import com.guigu.qingcheng.pojo.vo.CaiFenVo;
import com.guigu.qingcheng.pojo.vo.CategoryReportVo;
import com.guigu.qingcheng.service.TbOrderItemService;
import com.guigu.qingcheng.utils.PageUtil;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*
* @author Jiang
* @date 2020/07/31
*/
@Slf4j
@RestController
@RequestMapping("/order/item")
@Api(tags = "TbOrderItemController")
public class TbOrderItemController {
    @Resource
    private TbOrderItemService tbOrderItemService;

    @ApiOperation(value= "查询近七天销售总额")
    @PostMapping("/sevendays")
    public R sevendays(@RequestBody CategoryReportVo categoryReportVo){
        Map sevendaysMoney =  tbOrderItemService.findTotalSalesonweek(categoryReportVo);
        return R.ok().data("sevendaysMoney",sevendaysMoney.get("money"));
    }

    @ApiOperation(value= "查询昨日销售总额")
    @GetMapping("/yesterday")
    public R yesterdaymoney(){
        //获取前一天时间
        LocalDate localDate = LocalDate.now().minusDays(1);
        Map yesterdayMoney =  tbOrderItemService.findTotalSales(localDate);
        if(yesterdayMoney==null){
            return R.ok().data("yesterdayMoney",0);
        }
        return R.ok().data("yesterdayMoney",yesterdayMoney.get("money"));
    }

    @ApiOperation(value= "查询今日销售总额")
    @GetMapping("/today")
    public R todaymoney(){
        //获取今日时间
        LocalDate localDate = LocalDate.now();
        Map todayMoney =  tbOrderItemService.findTotalSales(localDate);
        if(todayMoney==null){
            return R.ok().data("todayMoney",0);
        }
        return R.ok().data("todayMoney",todayMoney.get("money"));
    }


    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id) {

        TbOrderItem tbOrderItem = tbOrderItemService.findById(id);
        return R.ok().data("tbOrderItem",tbOrderItem);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public R list() {
        List<TbOrderItem>  list = tbOrderItemService.queryAll();
        return R.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages/{pageNum}/{pageSize}")
    public R listPages(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageUtil<TbOrderItem> pageUtils = tbOrderItemService.findListAll(pageNum, pageSize);

        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public R pageQueryByAll(@RequestBody TbOrderItem tbOrderItem, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageUtil<TbOrderItem> pageUtils = tbOrderItemService.pageQueryByAll(pageNum, pageSize, tbOrderItem);
        return R.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public R add(@RequestBody TbOrderItem tbOrderItem) {

        boolean b = tbOrderItemService.saveTbOrderItem(tbOrderItem);
        if(b){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public R update(@RequestBody TbOrderItem tbOrderItem) {
        boolean b = tbOrderItemService.updateTbOrderItem(tbOrderItem);

        if(b){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        boolean b = tbOrderItemService.deleteById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation(value = "拆分订单")
    @PostMapping("/caiFen")
    public R caiFen(@RequestBody List<CaiFenVo> caiFenVos) {
        tbOrderItemService.caiFen(caiFenVos);
        return R.ok();
    }


    @ApiOperation(value = "根据订单id获取商品信息")
    @GetMapping("/queryByOrderIdGoods/{orderId}")
    public R queryByOrderIdGoods(@PathVariable("orderId") String  orderId) {
        List<CaiFenDinDan> TbOrderItems= tbOrderItemService.queryByOrderIdGoods(orderId);

        for (CaiFenDinDan c :TbOrderItems) {
            String aa = c.getSpec().replace("{", "");
            String replace = aa.replace("}", "");
            c.setSpec(replace.replaceAll("'", ""));

        }
        return R.ok().data("TbOrderItems",TbOrderItems);
    }


    @ApiOperation(value = "添加商品到订单")
    @PostMapping("/addGoodsOrder")
    public R addGoodsOrder(@RequestBody TbOrderItem tbOrderItem) {
        tbOrderItemService.addGoodsOrder(tbOrderItem);
        return R.ok();
    }
    @GetMapping("/host")
    public R findHost() {
        List<Map> host = tbOrderItemService.findHost();
        return R.ok().data("host",host);
    }
    @GetMapping("/price")
    public R lessPrice() {
        List<Map> lessprice = tbOrderItemService.pricelower();
        return R.ok().data("lessprice",lessprice);
    }
    @GetMapping("/commodities")
    public R commodities() {
        List<Map> commoditieseqs = tbOrderItemService.Commoditieseqs();
        return R.ok().data("commoditieseqs",commoditieseqs);
    }
}
